package wx_app

import (
	"encoding/json"
	"fmt"
	"shejiao/pkg/util"
)

type Jscode2sessionResponse struct {
	Errcode    int32  `json:"errcode"`
	Openid     string `json:"openid"`
	Errmsg     string `json:"errmsg"`
	Unionid    string `json:"unionid"`
	SessionKey string `json:"session_key"`
}

func Jscode2session(appId, Secret, JsCode string) (Jscode2sessionResponse, []byte) {
	api := "https://api.weixin.qq.com/sns/jscode2session"
	api = fmt.Sprintf("%s?appid=%s&secret=%s&js_code=%s&grant_type=%s", api, appId, Secret, JsCode, "authorization_code")
	var response Jscode2sessionResponse
	v, e := util.Get(api)
	if e != nil {
		return response, v
	}

	e = json.Unmarshal(v, &response)
	return response, v
}
